//
//  OrderOnlineViewController.m
//  AirConditioner
//
//  Created by Sean Wang on 12-7-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "OrderOnlineViewController.h"
#import "OrderSuccessViewController.h"

@interface OrderOnlineViewController ()

@end

@implementation OrderOnlineViewController

@synthesize scrollView, detailsViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Buy Now";
        
        UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
        temporaryBarButtonItem.title = @"Back";
        self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.scrollView.contentSize = CGSizeMake(320, 547);
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [activeField resignFirstResponder];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)orderMyAC:(id)sender
{
    if(orderSuccessViewController == nil){
        orderSuccessViewController = [[OrderSuccessViewController alloc]
                                     initWithNibName:@"OrderSuccessViewController" bundle:nil];
    }
    
    [[self navigationController] pushViewController:orderSuccessViewController animated:YES];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]
                     CGRectValue].size;
    //CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]
    //                 CGRectValue];
    
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    
    //NSLog(@"field y=%f",activeField.frame.origin.y);
    //NSLog(@"activeField.frame.size.height=%f",activeField.frame.size.height);
    //NSLog(@"kbSize.height=%f",kbSize.height);
    //NSLog(@"kbRect.origin.y=%f",kbRect.origin.y);
    
    
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        //CGPoint scrollPoint = CGPointMake(0.0,
        //                                  activeField.frame.origin.y-kbSize.height);
        //NSLog(@"scrollPoint.y=%f",scrollPoint.y);
        //[scrollView setContentOffset:scrollPoint animated:YES];
        [scrollView scrollRectToVisible:activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {    
    [textField resignFirstResponder];
    return YES;    
}

@end
